using System;
using System.Collections;
using ParamHelp = Widgetsphere.Core.DataAccess.ParameterHelper;

namespace Widgetsphere.Core.DataAccess
{
	/// <summary>
	/// Summary description for StoredProcedureFactory.
	/// </summary>
	public partial class StoredProcedureFactory
	{
		private Hashtable mStoredProcedures;
		private static StoredProcedureFactory _instance = null;

		private StoredProcedureFactory()
		{
			mStoredProcedures = new Hashtable();
		}

		internal static StoredProcedureFactory GetInstance()
		{
			if (_instance == null)
			{
				_instance = new StoredProcedureFactory();
			}
			return _instance;
		}

		internal static StoredProcedure GetStoredProcedure(string connectionString, string storedProcedureName)
		{
			return GetInstance().PrivateGetStoredProcedure(connectionString, storedProcedureName);
		}

		private StoredProcedure PrivateGetStoredProcedure(string connectionString, string storedProcedureName)
		{
			lock(mStoredProcedures)
			{
				if (!mStoredProcedures.Contains(storedProcedureName))
				{
					StoredProcedure newProcedure = new StoredProcedure(connectionString, storedProcedureName);
					mStoredProcedures.Add(storedProcedureName, newProcedure);
				}
			}
			StoredProcedure existingProcedure = (StoredProcedure) mStoredProcedures[storedProcedureName];
			return existingProcedure;
		}
}
}
